import requests
from bs4 import BeautifulSoup as bs
from my_fake_useragent import UserAgent
import time
ua = UserAgent()
headers = { 'user-Agent':ua.random }
def get_img(url):
    res = requests.get(url,headers=headers)
    soup = bs(res.text,'html.parser')
    img_class = soup.find_all('div', {"class": "IEpfq"})

    i = 0
    for img_list in img_class:
        imgs = img_list.find_all("img")
        for img in imgs:
            src = img["src"]
            r = requests.get(src,stream=True)
            img_name = 'unsplash_'+str(i)+'.jpg'
            i+=1
            with open('./img/%s' % img_name, 'wb') as file:
                for chunk in r.iter_content(chunk_size=1024):
                    file.write(chunk)
            print('save as %s' % img_name)
if __name__ == '__main__':
    start = time.time()
    url = 'https://unsplash.com/'
    get_img(url)
    end = time.time()
    print('耗时',end - start)

